package com.heer.gymnasium.module.speedy.views;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.order.beans.OrderResponseBean;
import com.heer.gymnasium.module.order.views.OrderActivity;
import com.heer.gymnasium.module.reservation.beans.ReservationBookItemResponseBean;
import com.heer.gymnasium.module.reservation.beans.ReservationGridBean;
import com.heer.gymnasium.module.reservation.beans.ReservationItemResponseBean;
import com.heer.gymnasium.util.DialogUtils;
import com.heer.gymnasium.util.ScreenUtils;
import com.heer.gymnasium.util.StringUtils;
import com.heer.imagepick.DensityUtils;
import com.zhihuiyun.library.calendar.data.CalendarDate;
import com.zhihuiyun.library.calendar.fragment.CalendarViewFragment;
import com.zhihuiyun.library.calendar.fragment.CalendarViewPagerFragment;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/1/30
 * 快捷预定
 */

public class SpeedyReservationActivity extends BaseActivity {
    private static final int RES_ID_LAYOUT = R.layout.layout_speedy;
    private static final int RES_ID_GV = R.id.layout_speedy_gv;
    private static final int RES_ID_CONTENT = R.id.layout_speedy_content_tv;
    private static final int RES_ID_NEXT = R.id.layout_speedy_next_tv;

    private static final int ITEM = R.layout.item_reservation;
    private static final int ITEM_ROOT_ID = R.id.item_reservation_ll;
    private static final int ITEM_ICON_ID = R.id.item_reservation_icon_tv;
    private static final int ITEM_TV_ID = R.id.item_reservation_tv;
    private static final int ITEM_LINE_ID = R.id.item_reservation_line_v;
    private static final int ITEM_TIME_ID = R.id.item_reservation_time_tv;
    private static final int ITEM_TIME_ID_LINE = R.id.item_reservation_time_line_v;
    @BindView(RES_ID_GV)
    GridView gridView;
    @BindView(RES_ID_NEXT)
    TextView nextTextView;

    QuickTypeAdapter<ReservationGridBean> adapter;

    private List<ReservationGridBean> data;
    private Activity $this;

    public static void startActivity(Activity activity, String id) {
        Intent intent = new Intent(activity, SpeedyReservationActivity.class);
        intent.putExtra(EXTRA_ID, id);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_LAYOUT);
    }

    @Override
    protected void initView() {
        super.initView();
        data = new ArrayList<>();
        adapter = new QuickTypeAdapter<ReservationGridBean>(this, data, ITEM) {
            @Override
            public void bindData4View(final ViewHolder holder, ReservationGridBean data, int type, final int pos) {
                boolean isPlace = TextUtils.equals(data.getKey(), ReservationGridBean.TYPE_FIELD);
                boolean isTime = TextUtils.equals(data.getKey(), ReservationGridBean.TYPE_TIME);
                if (isPlace || isTime) {
                    holder.setVisibility(ITEM_ROOT_ID, View.GONE);
                    holder.setVisibility(ITEM_TIME_ID, View.VISIBLE);
                    holder.setText(ITEM_TIME_ID, data.getText());
                    if (isTime) {
                        holder.setVisibility(ITEM_TIME_ID_LINE, View.VISIBLE);
                    }

                    if (isPlace || pos == 0) {
                        holder.setVisibility(ITEM_TIME_ID_LINE, View.GONE);
                    }
                } else {
                    holder.setVisibility(ITEM_ROOT_ID, View.VISIBLE);
                    holder.setVisibility(ITEM_TIME_ID, View.GONE);
                    holder.setVisibility(ITEM_TIME_ID_LINE, View.GONE);
                    holder.setTextColor(ITEM_TV_ID, Color.BLACK);
                    holder.setText(ITEM_TV_ID, data.getText());
                    if (!TextUtils.isEmpty(data.getState())) {
//                        if (TextUtils.equals(data.getState(), ReservationGridBean.STATUS_SOLD)) {
//                            holder.setBg(ITEM_ROOT_ID, R.drawable.place_gray_bg);
//                            holder.setBg(ITEM_LINE_ID, R.color.black);
//                        } else
                        if (TextUtils.equals(data.getState(), ReservationGridBean.STATUS_FELLOW) || TextUtils.equals(data.getState(), ReservationGridBean.STATUS_MATCH)) {
                            holder.setBg(ITEM_ROOT_ID, R.drawable.place_purple_bg);
                            holder.setBg(ITEM_LINE_ID, R.color.yellow);
                            holder.setTextColor(ITEM_TV_ID, Color.WHITE);
                            holder.setBg(ITEM_ICON_ID, R.color.transparent);
//                            if (TextUtils.equals(data.getState(), ReservationGridBean.STATUS_FELLOW)) {
//                                holder.setText(ITEM_ICON_ID, "拼");
//                            } else {
//                                holder.setText(ITEM_ICON_ID, "约");
//                            }
                        } else if (TextUtils.equals(data.getState(), ReservationGridBean.STATUS_MINE)) {
                            holder.setBg(ITEM_ROOT_ID, R.drawable.place_orange_bg);
                            holder.setBg(ITEM_LINE_ID, R.color.black);
                        } else {
                            holder.setBg(ITEM_ROOT_ID, R.drawable.place_white_bg);
                            holder.setBg(ITEM_LINE_ID, R.color.yellow);

                        }
                    }
                }

                if (pos == SpeedyReservationActivity.this.data.size() - 1) {
                    if (countTotal() > 0) {
                        nextTextView.setTextColor(getResources().getColor(R.color.white));
                        nextTextView.setBackgroundResource(R.drawable.yellow_button_bg);
                    } else {
                        nextTextView.setTextColor(getResources().getColor(R.color.black));
                        nextTextView.setBackgroundResource(R.drawable.gray_button_bg);
                    }
                }
                holder.setLis(ITEM_ROOT_ID, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        setState(pos);
                    }
                });

            }
        };
        gridView.setAdapter(adapter);
    }

    @Override
    protected void loadData() {
        super.loadData();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, +1);
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd");
        getBookingList(format.format(date));
    }

    @OnClick(RES_ID_NEXT)
    public void onClickForNext() {
        if (countTotal() > 0) {
            CalendarActivity.startActivity(this, getIntent().getStringExtra(EXTRA_ID), data);
        } else {
            ToastUtil.show(this, "还未选择订场");
        }
    }

    /**
     * 计算总额
     */
    private int countTotal() {
        int totalSize = 0;
        for (ReservationGridBean item : data) {
            if (TextUtils.equals(ReservationGridBean.STATUS_MINE, item.getState())) {
                totalSize++;
            }
        }
        return totalSize;
    }

    /**
     * @param pos
     */
    private void setState(int pos) {
        ReservationGridBean bean = data.get(pos);
        if (TextUtils.equals(bean.getState(), ReservationGridBean.STATUS_SOLD)) {
            bean.setState(ReservationGridBean.STATUS_EMPTY);
        }
        if (data.get(pos).getState().equals(ReservationGridBean.STATUS_EMPTY)) {
            bean.setState(ReservationGridBean.STATUS_MINE);
        } else if (data.get(pos).getState().equals(ReservationGridBean.STATUS_MINE)) {
            bean.setState(ReservationGridBean.STATUS_EMPTY);
        }
        data.set(pos, bean);
        adapter.notifyDataSetChanged();
    }

    private void getBookingList(final String date) {
        String cateId = getIntent().getStringExtra(EXTRA_ID);
        mAppHelper.apiService().getBookingList(cateId, date).enqueue(new BaseCallback<ApiResponse<List<List<ReservationBookItemResponseBean>>>>() {
            @Override
            public void onSuccess(Call<ApiResponse<List<List<ReservationBookItemResponseBean>>>> call, Response<ApiResponse<List<List<ReservationBookItemResponseBean>>>> response) {
                if (response.body().getData() != null && response.body().getData().size() > 0) {
                    if (response.body().getData().get(0) != null) {
                        int column = response.body().getData().get(0).size();
                        gridView.setNumColumns(column);
                    }
                }
                data.clear();
                data.addAll(ReservationGridBean.dataChange(response.body().getData()));
                adapter.notifyDataSetChanged();
            }
        });
    }
}
